<template>
  <div class="flex flex-col h-full overflow-auto">
    <div class="flex justify-between">
      <a-button type="link" class="px-0 !py-0" @click="onCreateLsh">生成流水号</a-button>
      <div>
        <a-button class="mr-2" @click="onSave">保存</a-button>
        <a-button v-if="formState.status === '0'" type="primary" class="mr-2" @click="onAudit">审核</a-button>
        <a-button v-if="formState.status === '1'" type="primary" class="mr-2" @click="onChanelAudit">撤审</a-button>
        <a-button class="mr-2">预览</a-button>
        <a-button class="mr-2">打印</a-button>
        <a-button type="primary" class="mr-2">查看影像</a-button>
      </div>
    </div>
    <div class="flex flex-1">
      <a-card class="flex-1 mt-2" :body-style="{ height: '100%' }">
        <h3 class="text-center text-2xl">医技病历</h3>
        <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '100px' } }">
          <a-form-item name="risSn" label="流水号" class="w-1/2">
            <a-input v-model:value="formState.risSn" placeholder="请输入" class="w-[300px]" />
          </a-form-item>
          <a-form-item name="examineTime" label="检查日期" class="w-1/2">
            <a-date-picker v-model:value="formState.examineTime" format="YYYY-MM-DD" value-format="YYYY-MM-DD" style="width: 300px" />
          </a-form-item>
          <a-form-item name="auditEmpName" label="报告医生" class="w-1/2">
            <SelectTable v-model:value="formState.auditEmpName" type="员工" page-size="999" class-name="w-[300px]" disabled />
          </a-form-item>
          <a-form-item name="auditTime" label="报告日期" class="w-1/2">
            <a-date-picker v-model:value="formState.auditTime" format="YYYY-MM-DD" value-format="YYYY-MM-DD" style="width: 300px" disabled />
          </a-form-item>
          <a-form-item name="risSn" label="临床诊断" class="w-full">
            <a-input v-model:value="formState.risSn" placeholder="请输入" disabled />
          </a-form-item>
        </a-form>
      </a-card>
    </div>
  </div>
</template>
<script setup>
import { message } from 'ant-design-vue'
import useUserStore from '@/store/user'
import userStore from '../../store'

const { empId, empName } = useUserStore()
const store = userStore()
const formRef = ref(null)
const formState = ref({})

const getData = async () => {
  const { data, code } = await post('/ris/record/getRisRecord', { appSn: store.currentPatient.appSn })
  if (code === 0) {
    formState.value = data || {}
    // status 0：未审核 1：已审核 2：已作废 3:已打印报告
    if (formState.value.status === '0') {
      formState.value.auditEmpName = empName
      formState.value.auditEmpId = empId
    }
  }
}

const onCreateLsh = async () => {
  const { data, code, msg } = await post('/ris/record/doGenerateRisRecordLsh', { ...formState.value })
  if (code !== 0) return message.error(msg || '操作失败')
  formState.value.risSn = data?.risSn
}
const onSave = async () => {
  const { code, msg } = await post('/ris/record/doSaveRisRecord', { ...formState.value })
  if (code !== 0) return message.error(msg || '操作失败')
  message.success(msg || '操作成功')
  getData()
}
const onAudit = async () => {
  const { code, msg } = await post('/ris/record/doAuditRisRecord', { ...formState.value })
  if (code !== 0) return message.error(msg || '操作失败')
  message.success(msg || '操作成功')
  getData()
}
const onChanelAudit = async () => {
  const { code, msg } = await post('/ris/record/doCancelAuditRisRecord', { ...formState.value })
  if (code !== 0) return message.error(msg || '操作失败')
  message.success(msg || '操作成功')
  getData()
}

watch(
  () => store.currentPatient,
  (val) => {
    if (val.brId) {
      getData()
    }
  }
)
</script>
